package com.situ.stl.Service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.situ.stl.Service.MemberService;
import com.situ.stl.dao.MemberMapper;
import com.situ.stl.model.Member;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class MemberServiceImpl implements MemberService {
    MemberMapper memberMapper;

    @Autowired
    public void setMemberMapper(MemberMapper memberMapper) {
        this.memberMapper = memberMapper;
    }

    //分页查询
    @Override
    public Page<Member> findAll(Page<Member> page, Member member) {
        return memberMapper.findAll(page, member);
    }

    //通过id查询
    @Override
    public List<Member> getByIds(List<Long> ids) {
        return memberMapper.selectById(ids);
    }

    @Override
    public int deleteByIds(List<Integer> ids) {
        return memberMapper.deleteByIds(ids);
    }

    //新增会员
    @Override
    public boolean save(Member member) {
        //如果余额为null，默认设为0
        if (member.getBalance() == null) {
            member.setBalance(BigDecimal.ZERO);
        }
        // 调用Mapper层插入方法，返回是否插入成功（影响行数是否为1）
        return memberMapper.insert(member) == 1;
    }

    //更新
    @Override
    public boolean update(Member member) {
        return memberMapper.updateById(member) == 1;
    }

    @Override
    public boolean recharge(Integer id, BigDecimal amount) {
        //充值金额为正数
        if (amount.compareTo(BigDecimal.ZERO) <= 0) {
            return false;
        }
        return memberMapper.recharge(id, amount) > 0;
    }

    @Override
    public boolean consume(Integer id, BigDecimal amount) {
        // 消费金额为正数
        if (amount.compareTo(BigDecimal.ZERO) <= 0) {
            return false;
        }
        return memberMapper.consume(id, amount) > 0;
    }

}
